/*
 * Inf-Bazen
 */
package fh.component;

import java.sql.*;

/**
 * A simple data source for getting database connections.
 */
public class DataSource {

    private static String dbserver;
    private static String database;
    private static String username;
    private static String password;
    private static Connection activeConn;

    /**
     * Initializes the data source. Checks if MySQL Driver is found contains the
     * database driver, Fill variabels dbserver, database, username, and
     * password
     *
     * TODO get variabels from a configuration file!!! Hardcoded is a bad code
     */
    public static void main(String args[]) throws SQLException {
        init();
        createConnection();


    }

    private static void init() {
        try {
            String driver = "com.mysql.jdbc.Driver";
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            System.out.println(e);
        }


        dbserver = "www.db4free.net:3306";
        database = "fullhouse23";
        username = "fullhouse";
        password = "groep23";


    }

    /**
     * Gets a connection to the database.
     *
     * @return the database connection
     */
    public static Connection getConnection() throws SQLException {
        if (activeConn == null) {
            init();
            activeConn = createConnection();
        }
        return activeConn;

    }

    private static Connection createConnection() throws SQLException {

        String connectionString = "jdbc:mysql://" + dbserver + "/" + database + "?"
                + "user=" + username + "&password=" + password;

        return DriverManager.getConnection(connectionString);
    }

    public static int executeUpdateQuery(String query) {
        PreparedStatement stmt;
        try {
            stmt = activeConn.prepareStatement(query);
            return stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Method executeUpdateQuery: Query bevat een fout.\nQuery: " + query);
            System.out.println(e.getMessage() + "\n" + e.getSQLState() + "\n" + e.getErrorCode());
        }
        return -1;
    }

    public static ResultSet executeSelectQuery(String query) {
        ResultSet rs = null;
        try {
            Statement stmt = activeConn.createStatement();
            rs = stmt.executeQuery(query);

            return rs;
        } catch (SQLException e) {
            System.out.println("Method executeSelectQuery: Query bevat een fout.\nQuery: " + query);
            System.out.println(e.getMessage() + "\n" + e.getSQLState() + "\n" + e.getErrorCode());
        }
        return null;
    }

    public static ResultSet executeSelectWithIdQuery(String query, int id) {
        try {
            PreparedStatement stmt = activeConn.prepareStatement(query);
            stmt.setInt(1, id);
            System.out.println(stmt.toString());
            ResultSet rs = stmt.executeQuery();
            return rs;
        } catch (SQLException e) {
            System.out.println("Method executeSelectWithIdQuery: Query bevat een fout.\nQuery: " + query);
            System.out.println(e.getMessage() + "\n" + e.getSQLState() + "\n" + e.getErrorCode());
            return null;
        }

    }

    public static int executeDeleteQuery(String query, int id) {
        try {
            PreparedStatement stmt = activeConn.prepareStatement(query);
            stmt.setInt(1, id);
            return stmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("Method executeUpdateQuery: Query bevat een fout.\nQuery: " + query);
            System.out.println(e.getMessage() + "\n" + e.getSQLState() + "\n" + e.getErrorCode());
        }
        return -1;
    }
}
